package net.sangpil.applications.viewer
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	import flash.net.FileFilter;
	
	import net.sangpil.applications.viewer.event.ViewerMapEvent;
	import net.sangpil.classes.events.SimpleLoaderEvent;
	import net.sangpil.classes.net.SimpleLoader;

	[Event(name = "map_loaded", type = "net.sangpil.applications.viewer.event.ViewerMapEvent")]
	public class ViewerMap extends Sprite
	{
		private var _naviSkin:Navi_imgViewer;
		private var _skinW:Number;
		private var _skinH:Number;
		
		private var _file:File;
		private var _fileFilter:FileFilter;
		
		private var _currentURL:String;
		
		private var _imgLoader:SimpleLoader;
		private var _mapImage:Bitmap;
		
		public function ViewerMap(width:Number, height:Number = 14.25, firstURL:String = null)
		{
			super();
			_skinW = width;
			_skinH = height;
			if(firstURL)
			{
				_currentURL = firstURL;
			} 
			
			this.addEventListener(Event.ADDED_TO_STAGE, onStage);
		}
		
		private function onStage(e:Event):void
		{
			this.init();	
		}
		
		private function init():void
		{
			_naviSkin = new Navi_imgViewer();
			this.setControlPosition(_skinW, 1);
			this.addChild(_naviSkin);
			
			this._fileFilter = new FileFilter("Image File", ".jpg;*.gif;*.png");
			this._file = new File();
			_file.addEventListener(Event.SELECT, onSelected);
			
			this._imgLoader = new SimpleLoader();
			
			this._naviSkin.btn_browse.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
			this._naviSkin.btn_browse.buttonMode = true;
			
			if(_currentURL)
			{
				this._imgLoader.addEventListener(SimpleLoaderEvent.COMPLETED, imageLoaded);
				this._imgLoader.load(_currentURL);
			}
		}
		
		public function setControlPosition(w:Number, h:Number, selectedStr:String = "Map Image : Open Browser"):void
		{
			this._naviSkin.y = h;
			this._naviSkin.bg_second.width = w;
			this._naviSkin.btn_browse.width = w;
			this._naviSkin.txt_url.width = w-10; 
			this._naviSkin.txt_url.text = selectedStr;
			//this.skin.txt_url.autoSize = TextAlign.LEFT;
		}
		
		private function onSelected(e:Event):void
		{
			var temp:File = e.target as File;
			trace("url : ", temp.url);
			
			this._imgLoader.addEventListener(SimpleLoaderEvent.COMPLETED, imageLoaded);
			this._imgLoader.load(temp.url);
		}
		
		private function imageLoaded(e:SimpleLoaderEvent):void
		{
			/* add bottom building to scene.basicObject */
			if(this._mapImage)
			{
				this.removeChild(_mapImage);
				this._mapImage = null;
			}
			this._mapImage = e.content as Bitmap;
			
			this._mapImage.x = 0;
			this._mapImage.y = 17.25;
			var temp_Ratio:Number = this._skinW/this._mapImage.width;
			this._mapImage.width = this._mapImage.width * temp_Ratio;
			this._mapImage.height = this._mapImage.height * temp_Ratio;
			//this._mapImage.height = 
			this.addChild(_mapImage);
			
			this.dispatchEvent(new ViewerMapEvent(ViewerMapEvent.MAP_LOADED));
		}
		
		public function get bitmapData():BitmapData
		{
			var bmd:BitmapData = this._mapImage.bitmapData;
			return bmd;
		} 
		
		private function onDown(e:MouseEvent):void
		{
			switch(e.currentTarget.name)
			{
				case "btn_play": 
					
				break;
				case "btn_pause":
					
				break;
				case "bar_progress": 
					
				break;
				case "btn_browse": 
					this._file.browse([this._fileFilter]);	
				break;
			}
		}
		
	}
}